<?php
namespace app\shopping\controller;

use think\Controller;
/**
 * 商品信息
 */
class Index extends Controller
{
    public function __construct(){
        parent::__construct();
        $this->ProductModel = model('Product.Product');
        $this->TypeModel = model('Product.Type');
        $this->BusinessModel = model('Business.User');
        $this->CartModel = model('Product.Cart');
        $this->BusinessModel = model('Business.User');
        $this->CommentModel = model('Product.Comment');
    }


    /**
     * 请求主页数据
     */
    public function index()
    {
        if($this->request->isPost()){
            // 轮播图
            $top = $this->ProductModel->where(['flag' => '3'])->limit(5)->select();

            // 分类
            $type = $this->TypeModel->limit(8)->select();

            // 新品首页
            $news = $this->ProductModel->where(['flag' => '1'])->limit(8)->select();

            $data = [
                'top' => $top,
                'type' => $type,
                'news' => $news
            ];

            $this->success('返回首页数据',null,$data);
            exit;
        
        }
    }


    /**
     * 请求分类列表
     */
    public function type(){
        if($this->request->isPost()){
            $list = $this->TypeModel->select();

            if($list){
               $this->success('返回分类列表成功',null,$list); 
               exit;
            }else{
                $this->error('暂无分类');
                exit;
            }

            
        }
    }

    /**
     * 获取商品列表
     *
     * @ApiTitle    (获取商品列表)
     * @ApiSummary  (获取商品列表)
     * @ApiMethod   (POST)
     * @ApiParams   (name="typeid", type="int", required=false, description="商品分类ID")
     * @ApiParams   (name="flag", type="int", required=false, description="0-全部商品,1-新品,2-热销,3-推荐")
     * @ApiParams   (name="sort", type="string", required=false, description="createtime-按上架时间,price-按价格,stock-按库存")
     * @ApiParams   (name="by", type="string", required=false, description="desc-降序,asc-升序")
     * @ApiParams   (name="keywords", type="string", required=false, description="搜索关键字")
     * @ApiParams   (name="page", type="int", required=false, description="页码")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function list(){
        if($this->request->isPOST()){
            $page = $this->request->param('page',1,'trim');
            $typeid = $this->request->param('typeid',0,'trim');
            $flag = $this->request->param('flag','0','trim');
            $sort = $this->request->param('sort','createtime','trim');
            $by = $this->request->param('by','desc','trim');
            $keywords = $this->request->param('keywords','','trim');
            $limit = 8;

            // 偏移量
            $offset = ($page-1) * $limit;
            
            // 查询分类名字
            $TypeName = $this->TypeModel->where(['id' => $typeid])->value('name');
            $TypeName = empty($TypeName) ? '全部分类' : $TypeName;

            $where = [];
            // 关键词不为空
            if(empty($keywords)){
                $where['name'] = ['LIKE',"%$keywords%"];
            }

            // 分类筛选
            if($typeid){
                $where['typeid'] = $typeid;
            }

            // 标签筛选
            if($flag != '0'){
                $where['flag'] = $flag;
            }

            $list = $this->ProductModel
                ->where($where)
                ->order($sort,$by)
                ->limit($offset,$limit)
                ->select();

            $data = [
                'TypeName' => $TypeName,
                'list' => $list
            ];

            if($list){
                $this->success('返回数据成功',null,$data);
                exit;
            }else{
                $this->error('暂无更多商品');
                exit;
            }

        }
    }

    /**
     * 商品详情
     *
     * @ApiTitle    (商品详情)
     * @ApiSummary  (商品详情)
     * @ApiMethod   (POST)
     * @ApiParams   (name="proid", type="int", required=true, description="商品ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function product(){
        if($this->request->isPOST()){
            $proid = $this->request->param('proid',0,'trim');
            
            $product = $this->ProductModel
                    ->with(['type','unit'])
                    ->find($proid);

            if($product){
                $this->success('返回数据成功',null,$product);
                exit;
            }else{
                $this->error('无商品信息');
                exit;
            }
        }
    }

    /**
     * 商品购物车数量
     *
     * @ApiTitle    (商品购物车数量)
     * @ApiSummary  (商品购物车数量)
     * @ApiMethod   (POST)
     * @ApiParams   (name="busid", type="int", required=true, description="客户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function count(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');

            $business = $this->BusinessModel->find($busid);
            // 用户是否存在
            if(!$business){
                $this->error('用户不存在');
                exit;
            }

            // 找出该客户的购物车的数量
            $count = $this->CartModel->where(['busid'=>$busid])->sum('nums');

            $this->success('返回购物车数量',null,$count);
            exit;
        }
    }

    /**
     * 商品分类显示商品列表
     *
     * @ApiTitle    (商品分类显示商品列表)
     * @ApiSummary  (商品分类显示商品列表)
     * @ApiMethod   (POST)
     * @ApiParams   (name="typeid", type="int", required=true, description="商品分类ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function productlist(){
        if($this->request->isPOST()){
            $typeid = $this->request->param('typeid',0,'trim');
            // var_dump($typeid);
            // exit;
            $product = $this->ProductModel->with(['type'])->where(['typeid'=>$typeid])->select();

            
            if($product){
                $this->success('返回商品成功',null,$product);
            }else{
                $this->error('暂无更多商品');
            }
           
        }
    }

    /**
     * 评论
     *
     * @ApiTitle    (评论)
     * @ApiSummary  (评论)
     * @ApiMethod   (POST)
     * @ApiParams   (name="proid", type="int", required=true, description="商品ID")
     * @ApiParams   (name="busid", type="int", required=true, description="用户ID")
     * @ApiReturnParams   (name="code", type="integer", required=true, sample="0")
     * @ApiReturnParams   (name="msg", type="string", required=true, sample="返回成功")
     * @ApiReturnParams   (name="data", type="object", sample="{'user_id':'int','user_name':'string','profile':{'email':'string','age':'integer'}}", description="扩展数据返回")
     * @ApiReturn   ({
        'code':'1',
        'msg':'返回成功'
    * })
    */
    public function comment(){
        if($this->request->isPost()){
            $busid = $this->request->param('busid',0,'trim');
            $proid = $this->request->param('proid',0,'trim');

            // 查询客户
            $business = $this->BusinessModel->find($busid);
            if(!$business){
                $this->error('用户未知');
                exit;
            }

            // 查询商品
            $product = $this->ProductModel->find($proid);
            if(!$product){
                $this->error('商品信息未知');
                exit;
            }

            $where = [
                'proid' => $proid
            ];
            // 查询评论
            $comment = $this->CommentModel
                    ->with(['business'])
                    ->where($where)
                    ->select();

            if(!$comment){
                $this->success('返回评论');
                exit;
            }else{
                $this->success('返回评论',null,$comment);
                exit;
            }
        }
    }

}